package com.lcn.comm.util;

import java.util.List;
import java.util.concurrent.*;

/**
 * <p>共享线程池工具类</p>
 *
 * @author 张峰 zfvip_it@163.com
 * @createTime: 2017/12/4 13:05
 */
public class PoolUtil {

    /**
     * 创建线程池
     */
    private static ThreadPoolExecutor pool = new ThreadPoolExecutor(30, Runtime.getRuntime().availableProcessors() * 30,
            1L, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>()) {
        @Override
        public void shutdown() {//避免线程池被人为关闭
        }

        @Override
        public List<Runnable> shutdownNow() {//避免线程池被人为关闭
            return null;
        }
    };

    /**
     * 返回线程池
     *
     * @return
     */
    public static ExecutorService getPool() {
        return pool;
    }

    /**
     * 返回指定核心线程大小的线程池
     *
     * @param corePoolSize
     * @return
     */
    public static ExecutorService getPoolSize(int corePoolSize) {
        return Executors.newFixedThreadPool(corePoolSize);
    }

    public static void main(String[] args) {
        System.out.println("开始数量" + PoolUtil.pool.getCorePoolSize());
        new Thread(new Runnable() {
            public void run() {
                System.out.println("新增核心数量前10=" + PoolUtil.pool.getCorePoolSize());
                PoolUtil.pool.setCorePoolSize(10);
                System.out.println("新增核心数量后10=" + PoolUtil.pool.getCorePoolSize());
            }
        }).start();

        new Thread(new Runnable() {
            public void run() {
                System.out.println("新增核心数量前20=" + PoolUtil.pool.getCorePoolSize());
                PoolUtil.pool.setCorePoolSize(20);
                System.out.println("新增核心数量后20=" + PoolUtil.pool.getCorePoolSize());
            }
        }).start();

        System.out.println("结束数量" + PoolUtil.pool.getCorePoolSize());
    }
}
